package fx.samples.comboboxsample;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ComboBoxSample extends Application {
	public static void main(final String[] args) {
		launch(args);
	}

	final Button button = new Button ("Send");
	final Label notification = new Label ();
	final TextField subject = new TextField("");
	final TextArea text = new TextArea ("");


	String address = " ";

	@Override public void start(final Stage stage) {
		stage.setTitle("ComboBoxSample");
		final Scene scene = new Scene(new Group(), 450, 250);

		final ComboBox<String> emailComboBox = new ComboBox<>();
		emailComboBox.getItems().addAll(
				"jacob.smith@example.com",
				"isabella.johnson@example.com",
				"ethan.williams@example.com",
				"emma.jones@example.com",
				"michael.brown@example.com"
				);
		emailComboBox.setPromptText("Email address");
		emailComboBox.setEditable(true);
		emailComboBox.valueProperty().addListener((ChangeListener<String>) (ov, t, t1) -> address = t1);

		final ComboBox<String> priorityComboBox = new ComboBox<>();
		priorityComboBox.getItems().addAll(
				"Highest",
				"High",
				"Normal",
				"Low",
				"Lowest"
				);
		priorityComboBox.setValue("Normal");
		priorityComboBox.setCellFactory(
				param -> {
					final ListCell<String> cell = new ListCell<String>() {
						{
							super.setPrefWidth(100);
						}
						@Override public void updateItem(final String item,
								final boolean empty) {
							super.updateItem(item, empty);
							if (item != null) {
								setText(item);
								if (item.contains("High"))
									setTextFill(Color.RED);
								else if (item.contains("Low"))
									setTextFill(Color.GREEN);
								else
									setTextFill(Color.BLACK);
							} else
								setText(null);
						}
					};
					return cell;
				});


		button.setOnAction(e -> {
			if (emailComboBox.getValue() != null &&
					!emailComboBox.getValue().toString().isEmpty()){
				notification.setText("Your message was successfully sent"
						+ " to " + address);
				emailComboBox.setValue(null);
				if (priorityComboBox.getValue() != null &&
						!priorityComboBox.getValue().toString().isEmpty())
					priorityComboBox.setValue(null);
				subject.clear();
				text.clear();
			} else
				notification.setText("You have not selected a recipient!");
		});

		final GridPane grid = new GridPane();
		grid.setVgap(4);
		grid.setHgap(10);
		grid.setPadding(new Insets(5, 5, 5, 5));
		grid.add(new Label("To: "), 0, 0);
		grid.add(emailComboBox, 1, 0);
		grid.add(new Label("Priority: "), 2, 0);
		grid.add(priorityComboBox, 3, 0);
		grid.add(new Label("Subject: "), 0, 1);
		grid.add(subject, 1, 1, 3, 1);
		grid.add(text, 0, 2, 4, 1);
		grid.add(button, 0, 3);
		grid.add (notification, 1, 3, 3, 1);

		final Group root = (Group)scene.getRoot();
		root.getChildren().add(grid);
		stage.setScene(scene);
		stage.show();

	}
}